package com.mrd.gtimp.business.service;

import com.mrd.base.BaseBiz;
import com.mrd.constant.BaseTypeConstant;
import com.mrd.constant.SystemConstants;
import com.mrd.exception.SaveException;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.business.entity.Booking;
import com.mrd.gtimp.business.entity.Goods;
import com.mrd.spring.repository.BaseRepository;
import com.mrd.util.date.DateUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */
@Service
public class BookingBiz extends BaseBiz<Booking> {

    @Resource
    private GoodsBiz goodsBiz;
    @Resource
    private ContacterBiz contacterBiz;


    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("bookingDao")
    public void setBaseRepository(BaseRepository<Booking, String> baseRepository) {
        super.baseRepository = baseRepository;
    }


    public void updateValidatePass(String id) throws Exception {
        User user = (User) HttpUtils.getRequest().getServletContext()
                .getAttribute(SystemConstants.CURRENT_USER);
        String validateDateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String sql = "update T_BIZ_BASE_BUSINESS set VALIDATOR_ID='" + user.getId()
                + "',  VALIDATE_TIME=to_date('" + validateDateStr
                + "','yyyy-mm-dd HH24:MI:SS') where ID='" + id + "'";
        baseRepository.updateBySql(sql);

        sql = "update T_BIZ_BOOKING set STATE = '" + BaseTypeConstant.BOOKINGSTATE_02
                + "' where ID='" + id + "'";
        baseRepository.updateBySql(sql);
    }


    public void updateValidateUpload(String id) throws Exception {
        System.out.println(this.getClass().getName() + "-->updateValidateUpload方法开发中");
    }


    public void updateValidateReject(String id) throws Exception {
        User user = (User) HttpUtils.getRequest().getSession()
                .getAttribute(SystemConstants.CURRENT_USER);
        String validateDateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String sql = "update T_BIZ_BASE_BUSINESS set VALIDATOR_ID='" + user.getId()
                + "',  VALIDATE_TIME=to_date('" + validateDateStr
                + "','yyyy-mm-dd HH24:MI:SS') where ID='" + id + "'";
        baseRepository.updateBySql(sql);

        sql = "update T_BIZ_BOOKING set STATE = '" + BaseTypeConstant.BOOKINGSTATE_03
                + "' where ID='" + id + "'";
        baseRepository.updateBySql(sql);
    }


    public void updateBizConfirm(String id) throws Exception {
        String validateDateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String sql = "update T_BIZ_BASE_BUSINESS set CONFIRM_TIME=to_date('" + validateDateStr
                + "','yyyy-mm-dd HH24:MI:SS') where ID='" + id + "'";
        baseRepository.updateBySql(sql);

        sql = "update T_BIZ_BOOKING set STATE = '" + BaseTypeConstant.BOOKINGSTATE_01
                + "' where ID='" + id + "'";
        baseRepository.updateBySql(sql);
    }


    public void saveOrUpdateEntities(List<Booking> entities) throws SaveException {
        baseRepository.saveOrUpdateEntities(entities);
    }

	/*
     * (non-Javadoc)
	 * @see
	 * com.mrd.gtimp.business.biz.BaseBusinessBiz#saveBusiness(com.mrd.gtimp.business.pojo.BaseBusiness
	 * )
	 */

    public void saveBusiness(Booking booking, List<String> properties) throws Exception {
        contacterBiz
                .saveConcater(booking.getShipper(), booking.getCustomer(), booking.getCreator());
        contacterBiz.saveConcater(booking.getConsignee(), booking.getCustomer(), booking
                .getCreator());
        List<Goods> goodes = booking.getGoodses();
        if (goodes != null) {
            for (Goods goods : goodes) {
                goods.setBooking(booking);
                if (StringUtils.isEmpty(goods.getId()) || goods.getId().length() < 3) {
                    goods.setId(null);
                }
            }
        }
        if (StringUtils.isEmpty(booking.getId())) {
            booking.setId(null);
            baseRepository.saveOrupdate(booking);
        } else {
            // 先删除与该票单关联的goods，再进行插入操作
            StringBuilder sql = new StringBuilder();
            sql.append("delete from T_BIZ_GOODS g where g.BOOKING_ID = '" + booking.getId() + "' ");
            if (null != booking.getGoodses() && booking.getGoodses().size() > 0) {
                sql.append(" and g.ID not in(");
                for (Goods s : booking.getGoodses()) {
                    sql.append("'" + s.getId() + "',");
                }
                sql.deleteCharAt(booking.getGoodses().size() - 1);
                sql.append(")");
            }
            baseRepository.excuteSql(sql.toString(), null);
            this.updateEntity(booking, properties);
        }
    }


    public void updateValidatePass(String id, String validator) throws Exception {
        String validateDateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String sql = "update T_BIZ_BASE_BUSINESS set VALIDATOR_EXT='" + validator
                + "',  VALIDATE_TIME=to_date('" + validateDateStr
                + "','yyyy-mm-dd HH24:MI:SS') where ID='" + id + "'";
        baseRepository.updateBySql(sql);

        sql = "update T_BIZ_BOOKING set STATE = '" + BaseTypeConstant.BOOKINGSTATE_02
                + "' where ID='" + id + "'";
        baseRepository.updateBySql(sql);
    }


    public void updateValidateReject(String id, String validator) throws Exception {
        String validateDateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String sql = "update T_BIZ_BASE_BUSINESS set VALIDATOR_EXT='" + validator
                + "',  VALIDATE_TIME=to_date('" + validateDateStr
                + "','yyyy-mm-dd HH24:MI:SS') where ID='" + id + "'";
        baseRepository.updateBySql(sql);

        sql = "update T_BIZ_BOOKING set STATE = '" + BaseTypeConstant.BOOKINGSTATE_03
                + "' where ID='" + id + "'";
        baseRepository.updateBySql(sql);
    }


    public void updateValidateUpload(String id, String validator) throws Exception {
        String validateDateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String sql = "update T_BIZ_BASE_BUSINESS set VALIDATOR_EXT='" + validator
                + "',  VALIDATE_TIME=to_date('" + validateDateStr
                + "','yyyy-mm-dd HH24:MI:SS') where ID='" + id + "'";
        baseRepository.updateBySql(sql);

        sql = "update T_BIZ_BOOKING set STATE = '" + BaseTypeConstant.BOOKINGSTATE_04
                + "' where ID='" + id + "'";
        baseRepository.updateBySql(sql);
    }


    public void updateValidateFinish(String id, String validator) throws Exception {
        String validateDateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String sql = "update T_BIZ_BASE_BUSINESS set VALIDATOR_EXT='" + validator
                + "',  VALIDATE_TIME=to_date('" + validateDateStr
                + "','yyyy-mm-dd HH24:MI:SS') where ID='" + id + "'";
        baseRepository.updateBySql(sql);

        sql = "update T_BIZ_BOOKING set STATE = '" + BaseTypeConstant.BOOKINGSTATE_05
                + "' where ID='" + id + "'";
        baseRepository.updateBySql(sql);
    }


    public void updateValidateCancel(String id) throws Exception {
    }


    public void updateValidateApplyCancel(String id) throws Exception {
    }

}
